package cn.bdqn.controller;

import cn.bdqn.dto.Dto;
import cn.bdqn.pojo.ItripUser;
import cn.bdqn.service.TokenService;
import cn.bdqn.service.UserService;
import cn.bdqn.util.MD5Utils;
import cn.bdqn.vo.ItripTokenVO;
import cn.bdqn.vo.ItripUserRegInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;

/**
 * Created by Administrator on 2018/8/20.
 */
@Api(value = "API", basePath = "")
@Controller
public class UserController {
    @Resource
    private UserService userService;
    @Resource
    private TokenService tokenService;

    /**
     * 用户注册
     *
     * @param userInfo
     * @return
     */
    @ApiOperation(value = "用户注册", httpMethod = "POST",
            protocols = "HTTP", produces = "application/json",
            response = Dto.class, notes = "使用邮箱或者手机号注册 ")
    @RequestMapping(value = {"/api/doregister","/api/registerbyphone"}, method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Dto doRegister(
            @ApiParam(name = "userInfo", value = "用户注册信息实体", required = true)
            @RequestBody ItripUserRegInfo userInfo) {
        Dto dto=userService.selectItripUserRepeat(userInfo.getUserCode());
           if(!dto.getCode().equals("0")) {
               return dto;
           }

        return userService.regdit(userInfo);
    }

    /**
     * 用户激活
     *
     * @param username
     * @param activeCode
     * @return
     */
    @ApiOperation(value = "用户激活", httpMethod = "put",
            protocols = "HTTP", produces = "application/json",
            response = Dto.class, notes = "使用邮箱或者手机号激活")
    @RequestMapping(value = "/api/activate", method = RequestMethod.PUT, produces = "application/json")
    @ResponseBody
    public Dto doActive(@RequestParam(required = true) String user, @RequestParam(required = true) String code) {
        return userService.active(user, code);
    }

    /**
     * 用户使用邮箱重新激活
     *
     * @param userCode
     * @return
     */
    @ApiOperation(value = "用户重新激活邮箱", httpMethod = "POST",
            protocols = "HTTP", produces = "application/json",
            response = Dto.class, notes = "使用邮箱重新激活")
    @RequestMapping(value = "/activeEmail", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    Dto doActiveEmail(@RequestParam(required = true) String userCode) {
        return userService.activeEmail(userCode);
    }

    /**
     * 用户使用手机号重新激活
     *
     * @param userCode
     * @return
     */
    @ApiOperation(value = "用户重新激活手机号", httpMethod = "POST",
            protocols = "HTTP", produces = "application/json",
            response = Dto.class, notes = "使用手机号重新激活")
    @RequestMapping(value = "/activePhone", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    Dto doActivePhone(@RequestParam(required = true) String userCode) {
        return userService.activePhone(userCode);
    }


    /**
     * 账号密码登陆
     *
     * @param userCode
     * @param userPassword
     * @return
     */
    @ApiOperation(value = "登陆", httpMethod = "POST",
            protocols = "HTTP", produces = "application/json",
            response = Dto.class, notes = "通过账号密码登陆")
    @RequestMapping(value = "/api/dologin", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    Dto dologin(@RequestParam String name, @RequestParam String password, HttpServletRequest request) {
        ItripUser itripUser = new ItripUser();
        itripUser.setUserCode(name);
        itripUser.setUserPassword(MD5Utils.getMD5(password,6));
        Dto dto = userService.selectLogin(itripUser);
        //存到token里
//        String userAgent =request.getRequestHeader("User-Agent");
        if (dto.getData() != null) {
            String token = tokenService.getToken(request.getHeader("user-agent"), (ItripUser) dto.getData());
            tokenService.save(token, (ItripUser) dto.getData());
            ItripTokenVO itripTokenVO = new ItripTokenVO(token, Calendar.getInstance().getTimeInMillis() + 2 * 60 * 60 * 1000, Calendar.getInstance().getTimeInMillis());
            dto.setData(itripTokenVO);
        }
        return dto;
    }


    @ApiOperation(value = "验证token", httpMethod = "POST",
            protocols = "HTTP", produces = "application/json",
            response = Dto.class, notes = "验证token")
    @RequestMapping(value = "/tokenValidaty", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    Dto tokenValidty(HttpServletRequest request) {
        Dto dto = new Dto();
        if (tokenService.validaty(request.getHeader("user-agent"), request.getHeader("token"))) {
            dto.setCode("0");
            dto.setMsg("可以使用的token");
        } else {
            dto.setCode("10003");
            dto.setMsg("无效的token,重新登陆");
            dto.setSuccess("false");
        }
        return dto;
    }

    @ApiOperation(value = "退出登陆", httpMethod = "GET",
            protocols = "HTTP", produces = "application/json",
            response = Dto.class, notes = "验证token")
    @RequestMapping(value = "/api/logout", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    Dto logOut(HttpServletRequest request) {
        Dto dto = new Dto();
        String token = request.getHeader("token");
        if (tokenService.validaty(request.getHeader("user-agent"), request.getHeader("token"))) {
            boolean temp = tokenService.deleteToken(token);
            if (temp) {
                dto.setCode("0");
                dto.setMsg("退出成功");
            } else {
                dto.setCode("10001");
                dto.setMsg("退出失败");
                dto.setSuccess("false");
            }
        }
        return dto;
    }

    @ApiOperation(value = "刷新token", httpMethod = "POST",
            protocols = "HTTP", produces = "application/json",
            response = Dto.class, notes = "刷新token")
    @RequestMapping(value = "/reToken", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    Dto reToken(HttpServletRequest request) {
        Dto dto = new Dto();
        String token = null;
        try {
            token = tokenService.reload(request.getHeader("user-agent"), request.getHeader("token"));
        } catch (Exception e) {
            dto.setCode("10001");
            dto.setMsg(e.getMessage());
            dto.setSuccess("false");
        }
        ItripTokenVO itripTokenVO = new ItripTokenVO(token, Calendar.getInstance().getTimeInMillis() + 2 * 60 * 60 * 1000, Calendar.getInstance().getTimeInMillis());
        dto.setData(itripTokenVO);
        return dto;
    }

    /**
     * 检测用户是否可以z注册（是否已经注册）
     *
     * @param userCode
     * @return
     */
    @ApiOperation(value = "检测用户名是否重复", httpMethod = "POST",
            protocols = "HTTP", produces = "application/json",
            response = Dto.class, notes = "用户名是否可以使用")
    @RequestMapping(value = "/api/ckusr", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    Dto repeatUserCode(@RequestParam(required = true) String name) {
        return userService.selectItripUserRepeat(name);
    }


}
